/*
 * 处理weixin的业务逻辑
 * replay、支付、错误信息的通知等
 */
'use strict'


var config = require('./config');
var wechatApi = config.getWechat();
var Art = require('./app/api/art')

function* reply(next){
	var message = this.weixin;

	if(message.MsgType === 'event'){
		if(message.Event === 'subscribe'){
			if(message.EventKey) {
				console.log('扫描二维码关注：'+ message.EventKey +' '+ message.ticket);
			}
			this.body = '欢迎来到i优微信公众号\n'+
						'生活健康、人工智能、精彩活动...\n'+
						'赶快浏览嗨起来!';
		}else if(message.Event === 'unsubscribe'){
			this.body = '';
			console.log(message.FromUserName + ' 悄悄地走了...');
		}else if(message.Event === 'CLICK'){
			//点击自定义菜单
			if(message.EventKey === 'Welcome_art'){
				this.body = [{
					title:'欢迎投稿!',
					description:'您若看到优秀的文章,或您自己有深刻的感悟与大家分享,我们愿意为您搭台,发现更多合作机会',
					picUrl:'https://baikebcs.bdimg.com/adpic/%E6%A0%A1%E5%9B%AD%E6%8B%9B%E6%96%B0-%E5%8F%B3%E4%B8%8A-1.png',
					url:'http://www.ioyoyo.com/wechat/welart'
				}]
			}
		}
	}else if(message.MsgType === 'voice'){
		var vocieText = message.Recognition
		var arts = yield Art.searchByName(vocieText)
		if(!arts || arts.length === 0){
			movies = yield Art.searchByDouban(vocieText)
		}
		if(movies && movies.length>0){
			reply = []
			movies = movies.slice(0,10)
			movies.forEach(function(item,index){
				reply.push({
					picUrl:item.poster,
					url: '我们域名/jump/'+movie._id,
					description: item.title,
					title: item.title
				})
			})
		}else{
			reply="没有查询到"+content+"匹配数据"
		}
	}else if(message.MsgType === 'text'){
		var content = message.Content;
		var reply = '你说的话：“' + content + '”，我听不懂呀';
		if(content === '1'){
			reply = '金刚:骷髅岛';
		}
		else if(content === '2'){
			var data = yield wechatApi.uploadTempMaterial('image',__dirname + '/public/king.jpg');
			reply = {
				type:'image',
				mediaId:data.media_id
			}
		}
		else if(content === '3'){
			var data = yield wechatApi.uploadTempMaterial('voice',__dirname + '/public/aiyou.mp3');
			reply = {
				type:'voice',
				mediaId:data.media_id
			}
		}
		else if(content === '4'){
			reply = [{
				title:'金刚.骷髅岛',
				description:'南太平洋上的神秘岛屿——骷髅岛。史上最大金刚与邪恶骷髅蜥蜴的较量。',
				picUrl:'https://baikebcs.bdimg.com/adpic/%E6%A0%A1%E5%9B%AD%E6%8B%9B%E6%96%B0-%E5%8F%B3%E4%B8%8A-1.png',
				url:'http://www.piaohua.com/html/dongzuo/2017/0409/31921.html'
			}];
		}
		else if(content === '5'){
			var groups = yield wechatApi.getGroups();
			console.log('获取到如下分组：\n'+ JSON.stringify(groups));
		}
		else if(content === '6'){
			var msg = yield wechatApi.moveUsersToGroup(message.FromUserName,114);
			var groups = yield wechatApi.getGroups();
			console.log('获取到如下分组：\n'+ JSON.stringify(groups));
		}
		else if(content === '7'){
			var remark = yield wechatApi.updateUserRemark(message.FromUserName,'芒果屋里的猫');
			reply = "您的备注名已经被设置为："+remark;
		}
		else if(content === '8'){
			var data1 = yield wechatApi.fetchUserInfo(message.FromUserName);
			console.log(JSON.stringify(data1));
			var data2 = yield wechatApi.fetchUserInfo([message.FromUserName]);
			console.log(JSON.stringify(data2))
		}
		else if(content === '9'){
			var data1 = yield wechatApi.getUserOpenIds();
			console.log(JSON.stringify(data1));
			var data2 = yield wechatApi.getUserOpenIds(message.FromUserName);
			console.log(JSON.stringify(data2));
		}
		else if(content === '10'){
			var data = yield wechatApi.getMenu();
			console.log(JSON.stringify(data));
		}
		else if(content === '17'){
			var text = {
		      content:'这是群发消息测试唔~'
		   	};
			var msg = yield wechatApi.massSendMsg('text',text,114)
			console.log('msg:'+ JSON.stringify(msg))
		}
		else if(content === '18'){
			var data = yield wechatApi.getBatchMaterial({
				type:'video',
				offset: 0,
				count: 1
			})
			reply = {
				type:'video',
				mediaId: data.media_id,
				description: '环保小视频',
				title: '环保'
			}
		}else{
			var movies = yield Movie.searchByName(content)
			if(!movies || movies.length === 0){
				movies = yield Movie.searchByDouban(content)
			}
			if(movies && movies.length>0){
				reply = []
				movies = movies.slice(0,10)
				movies.forEach(function(item,index){
					reply.push({
						picUrl:item.poster,
						url: 'http://99287afa.ngrok.io/jump/'+item._id,
						description: item.title,
						title: item.title
					})
				})
			}else{
				reply="没有查询到"+content+"匹配数据"
			}
		}
		// ... 其他回复类型
		this.body = reply
	}
	yield next;
}

exports.reply = reply

	

